package com.example.dubbonacosuserconsumer.configuration;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.stereotype.Component;


@Activate(group = {CommonConstants.CONSUMER}) // 指定 Filter 应用于服务端和客户端
public class TokenConsumerFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println("TokenConsumerFilter========================");
        String token = "用户token,xixi"; // 从合适的地方获取 token，例如 Session、ThreadLocal 等
//        RpcContext.getContext().setAttachment("token", token);
//        RpcContext.getClientAttachment().setAttachment("token2", token);
        RpcContext.getClientAttachment().setAttachment("userToken", token);
//        RpcContext.getServerAttachment().setAttachment("token2", "your-token-value server-value");
//        System.out.println(RpcContext.getClientAttachment().getAttachment("token2"));
//        System.out.println(RpcContext.getServerAttachment().getAttachment("token2"));

        return invoker.invoke(invocation);
    }
}
